package com.twohand.admin.Service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.hash.Hashing;
import com.twohand.admin.Entity.ShortLink;
import com.twohand.admin.Mapper.ShortLinkMapper;
import com.twohand.admin.Service.ShortLinkService;
import com.twohand.admin.Util.Base62Utils;
import com.twohand.utils.Response.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class ShortLinkServiceImpl implements ShortLinkService {
    @Autowired
    ShortLinkMapper shortLinkMapper;
    @Override
    public String generateShortLink(String longLink) {
        long longLinkHash = Hashing.murmur3_32_fixed().hashString(longLink, StandardCharsets.UTF_8).padToLong();
        String shortLink = getShortLink(longLinkHash,longLink);
        if(StringUtils.isNotBlank(shortLink)){
            return shortLink;
        }
        return regenrateShortLink(longLink,longLinkHash);
    }

    @Override
    public String regenrateShortLink(String longLink, long longLinkHash) {
        long uniqueIdHash = Hashing.murmur3_32_fixed().hashLong(IdUtil.getSnowflakeNextId()).padToLong();
        String shortLink = Base62Utils.encodeToBase62String(Math.abs(longLinkHash - uniqueIdHash));
        if(!isShortLinkRepeated(shortLink)){
            ShortLink shortLink1 = new ShortLink();
            shortLink1.setShortLink(shortLink);
            shortLink1.setLongLink(longLink);
            shortLink1.setLongLinkHash(longLinkHash);
            saveShortLink(shortLink1);
            return shortLink;
        }
        return regenrateShortLink(longLink,longLinkHash);
    }

    @Override
    public Boolean saveShortLink(ShortLink shortLink) {
        return shortLinkMapper.insert(shortLink) > 0;
    }

    @Override
    public String getShortLink(long longLinkHash, String longLink) {
        LambdaQueryWrapper<ShortLink> shortLinkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shortLinkLambdaQueryWrapper.eq(ShortLink::getLongLinkHash,longLinkHash)
                .eq(ShortLink::getLongLink,longLink);
        List<ShortLink> shortLink = shortLinkMapper.selectList(shortLinkLambdaQueryWrapper);
        if(shortLink.size()==0){
            return null;
        }
        return shortLink.get(0).getShortLink();
    }

    @Override
    public Boolean isShortLinkRepeated(String shortLink) {
        Wrapper<ShortLink> wrapper = Wrappers.lambdaQuery(ShortLink.class).eq(ShortLink::getShortLink,shortLink);
        return shortLinkMapper.selectCount(wrapper) > 0;
    }

    @Override
    public String getLongLink(String shortLink) {
        LambdaQueryWrapper<ShortLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShortLink::getShortLink,shortLink);
        List<ShortLink> shortLinks = shortLinkMapper.selectList(lambdaQueryWrapper);
        if(shortLinks.size()==0){
            throw new BaseException(500,"链接错误");
        }
        return shortLinks.get(0).getLongLink();
    }
}
